home *** CD-ROM | disk | FTP | other *** search
Java Source | 2004-03-20 | 7.7 KB | 254 lines |
- /*
- * Copyright (C) 2003 Bob Tantlinger
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- */
-
- import java.io.*;
- import java.util.*;
- import java.text.SimpleDateFormat;
-
- public class RSSWriter
- {
- final String RSS_VERSION_INFO =
- "<?xml version=\"1.0\" ?>\n"
- + "<rdf:RDF xmlns=\"http://purl.org/rss/1.0/\" "
- + "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
- + "xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xml:lang=\"en\">";
-
- BlogVariables blogVars;
- File rssFile;
- PrintWriter pw;
- String url = "";
- SimpleDateFormat sdf;
- int wordLen = 20;
-
- boolean isWriteFullEntry = true;
-
- public RSSWriter(File f, BlogVariables b, boolean writeFullEntries) throws IOException
- {
- sdf = new SimpleDateFormat();
- sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
- sdf.applyPattern("EEE, dd MMM yyyy HH:mm:ss z");
- isWriteFullEntry = writeFullEntries;
- pw = new PrintWriter(new FileWriter(f));
- rssFile = f;
- blogVars = b;
- }
-
- public void writeEntry(BlogEntry be, String arcPage) throws IOException
- {
- String title = replaceHTMLTags(be.getTitle());
- String entry;
- if (isWriteFullEntry)
- entry = replaceHTMLTags(be.getEntryText());
- else
- entry =
- removeHTMLTags(firstWords(be.getEntryText(), wordLen)) + "...";
-
- String arcUrl = blogVars.getArchivesUrl();
- if (!arcUrl.endsWith("/"))
- arcUrl += "/";
- arcUrl += arcPage + "#" + be.getID();
-
- pw.println("<item rdf:about=\"" + arcUrl + "\">");
- pw.println("<title>" + title + "</title>");
- pw.println("<link>" + arcUrl + "</link>");
- pw.println("<description>" + entry + "</description>");
- pw.println("<pubDate>" + sdf.format(be.getTimestamp()) + "</pubDate>");
- pw.println("</item>");
- }
-
- private String firstWords(String str, int n)
- {
- StringTokenizer st = new StringTokenizer(str);
- int count = 0;
- String words = "";
-
- while (st.hasMoreTokens() && count < n)
- {
- words += st.nextToken() + ' ';
- count++;
- }
-
- return words;
- }
-
- /*private String replaceHTMLTags(String s)
- {
- if(s.indexOf('<') < 0 && s.indexOf('>') < 0 && s.indexOf('&') < 0)
- return s;
-
- String l = "<";
- String r = ">";
-
- StringBuffer sb = new StringBuffer(s);
- for(int i = 0; i < sb.length(); i++)
- {
- if(sb.charAt(i) == '&')
- {
- sb.deleteCharAt(i);
- sb.insert(i, "&");
- }
- else if(sb.charAt(i) == '<')
- {
- sb.deleteCharAt(i);
- sb.insert(i, l);
- }
- else if(sb.charAt(i) == '>')
- {
- sb.deleteCharAt(i);
- sb.insert(i, r);
- }
- }
-
- return sb.toString();
- } */
-
- public String replaceHTMLTags(String string)
- {
- StringBuffer sb = new StringBuffer(string.length());
- // true if last char was blank
- boolean lastWasBlankChar = false;
- int len = string.length();
- char c;
-
- for (int i = 0; i < len; i++)
- {
- c = string.charAt(i);
- if (c == ' ')
- {
- // blank gets extra work,
- // this solves the problem you get if you replace all
- // blanks with , if you do that you loss
- // word breaking
- if (lastWasBlankChar)
- {
- lastWasBlankChar = false;
- sb.append(" ");
- }
- else
- {
- lastWasBlankChar = true;
- sb.append(' ');
- }
- }
- else
- {
- lastWasBlankChar = false;
- //
- // HTML Special Chars
- if (c == '"')
- sb.append(""");
- else if (c == '&')
- sb.append("&");
- else if (c == '<')
- sb.append("<");
- else if (c == '>')
- sb.append(">");
- //else if (c == '\n')
- // Handle Newline
- // sb.append("<br/>");
- else
- {
- int ci = 0xffff & c;
- if (ci < 160)
- // nothing special only 7 Bit
- sb.append(c);
- else
- {
- // Not 7 Bit use the unicode system
- sb.append("");
- sb.append(new Integer(ci).toString());
- sb.append(';');
- }
- }
- }
- }
- return sb.toString();
- }
-
- // added by John Montgomery - strips HTML tags from entries
- private String removeHTMLTags(String s)
- {
- if (s.indexOf('<') < 0 && s.indexOf('>') < 0 && s.indexOf('&') < 0)
- return s;
-
- StringBuffer buffer = new StringBuffer(s.length());
-
- int index = -1;
- while ((index = s.indexOf('<')) != -1)
- {
- String head = s.substring(0, index);
- String tail = s.substring(index);
- buffer.append(head);
- index = tail.indexOf('>');
- if (index != -1) // if it's -1 we're partway thru a tag
- {
- tail = tail.substring(index + 1);
- }
- else
- {
- //convert broken tags or trailing '<' to <
- //so we don't get stuck in an infinte loop
- tail = replaceHTMLTags(tail);
- }
- s = tail;
- }
- buffer.append(s); // add what ever is left over
- return replaceHTMLTags(buffer.toString());
- }
-
- public void writeHeader() throws IOException
- {
- String lastBuild = sdf.format(new Date());
- String url = blogVars.getBaseUrl();
- if (!url.endsWith("/"))
- url += "/";
-
- pw.println(RSS_VERSION_INFO);
- pw.println(
- "<channel rdf:about=\""
- + url
- + rssFile.getName()
- + "\">\n"
- + "<title>"
- + blogVars.getTitle()
- + "</title>\n"
- + "<link>"
- + url
- + blogVars.getMainPageFileName()
- + "</link>\n"
- + "<description>"
- + blogVars.getDescription()
- + "</description>\n"
- + "<lastBuildDate>"
- + lastBuild
- + "</lastBuildDate>\n"
- + "</channel>\n");
- }
-
- public void writeFooter() throws IOException
- {
- pw.println("</rdf:RDF>");
- }
-
- public void close() throws IOException
- {
- pw.close();
- }
- }
-